/*
 * @Author: 谢小康 2170673706@qq.com
 * @Date: 2023-12-13 17:39:50
 * @LastEditors: 谢小康 2170673706@qq.com
 * @LastEditTime: 2024-01-19 13:32:41
 * @FilePath: \my_bolg_admin\my_admin\src\store\premission.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from "pinia";
import { routerTotree,generateRouter,getMenulist } from "../util/premission.js";
import {getRouterList} from "../api/users.js";
export const usePremission = defineStore("premission",{
       // 数据存到store里刷新页面会重置，持久化就不会了
  persist: {
    enabled: true, //开启数据持久化
    strategies: [
      {
        key: "premission", //给一个要保存的名称
        storage: localStorage, //sessionStorage / localStorage 存储方式
      },
    ],
  },
    state:()=>({
        uid:3,
        hasAuth:false,
        menulist:[],   
        routerlist:[]
    }),
    getters:{
      getmenu(){
        return this.menulist
      },
      getrouterlist(){
        return this.routerlist
      }
    },
    actions:{
      
        // 获取用户权限
        async getUserRouters(){
            /* 获取当前的路由信息 */
            const currentRoterInfo = await getRouterList()
            /* 获取当前用户权限 */
            const userRouters =  routerTotree(currentRoterInfo.data.data.authRouterInfo); 
            /* 生成路由 */
            const generateRouters = generateRouter(userRouters);
            this.routerlist =generateRouters
            localStorage.setItem("routerlist",JSON.stringify(generateRouters))
            
        },
        createmenu(generateRouters){
          const menulist = getMenulist(generateRouters)
          this.menulist = menulist
          localStorage.setItem("menulist",JSON.stringify(menulist))
        }
    }
});